import random
import json
import pgzrun

IMG_SIZE = 80

WIDTH = IMG_SIZE * 9
HEIGHT = IMG_SIZE * 9

ROWS = 9
COLS = 9

BACK_COLOR = [0, 0, 0]

images = {}

for i in range(1, 10):
    images[i] = Actor('small/' + str(i))

def GetRowColFromIndex(idx):
    r = int(idx / ROWS)
    c = int(idx % ROWS)
    return r, c

with open(file='./output.json', mode='r', encoding='utf-8') as f:
    ftext = f.read()

jsonDict = json.loads(ftext)
board = jsonDict['map']

count = random.randint(10, 50)
print('count:', count)

nums = []

while len(nums) < count:
    idx = random.randint(0, ROWS * COLS - 1)
    if idx in nums:
        continue

    nums.append(idx)

for n in nums:
    r, c = GetRowColFromIndex(n)
    board[r][c] = 0

# for r in range(ROWS):
#     print(board[r])

def update():
    pass

def draw():
    screen.fill(tuple(BACK_COLOR))

    for r in range(ROWS):
        for c in range(COLS):
            x = c * IMG_SIZE + IMG_SIZE // 2
            y = r * IMG_SIZE + IMG_SIZE // 2
            num = board[r][c]
            if num > 0:
                img = images[num]
                img.pos = x, y
                img.draw()

pgzrun.go()